package home.my.base.web;

import home.my.base.model.BaseObj;
import home.my.base.service.BaseService;
import home.my.base.util.TypeUtil;

import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.Action;

public abstract class ModelAction<T extends BaseObj,S extends BaseService<T>> 
	extends SearchAction<T,S> {
	private T pojo;
	
	public ModelAction() {
		setClassFromFather();
	}

	@SuppressWarnings("unchecked")
	protected void setClassFromFather() {
		Class<?> c = TypeUtil.getClassType(this);
		if(c!=null){
			try {
				setPojo( (T)c.newInstance() );
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public String load(){
		pojo = this.getService().load(pojo.getPk());
		return Action.SUCCESS;
	}
	
	public String create(){
		this.getService().create(pojo);
		return Action.SUCCESS;
	}
	
	public String update(){
		this.getService().update(pojo);
		return Action.SUCCESS;
	}
	
	public String delete(){
		this.getService().delete(pojo);
		return Action.SUCCESS;
	}
	
	@JSON(serialize=false)
	public T getPojo() {
		return pojo;
	}
	public void setPojo(T pojo) {
		this.pojo = pojo;
	}
	
}
